package descentFly.away3D4.action
{
	import UIFree.FX;
	import UIFree.FXContainer;
	import UIFree.FXEvent;
	import UIFree.uif_Timer;
	import UIFree.uif_transition;
	
	import away3d.containers.ObjectContainer3D;
	
	import descentFly.away3D4.df_awayUtils;
	import descentFly.df_optionsObject;
	import descentFly.df_spaceObject;
	
	import flash.events.Event;
	import flash.events.IEventDispatcher;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import org.osmf.events.TimeEvent;
	
	import space.sp_utils;
	
	public class df_action extends df_optionsObject {
		private static const DEFAULT_FPS		: Number	= 16;
		private static const DEFAULT_CYCLE		: Boolean	= false;
		private static const DEFAULT_DURATION	: Number	= 1000;
		private static const DEFAULT_TRANSITION	: Function	= FX.DEFAULTTRANSITION;
		
		public static const FIELD_FPS			: String = 'fps';
		public static const FIELD_CYCLE			: String = 'cycle';
		public static const FIELD_DURATION		: String = 'duration';
		public static const FIELD_TRANSITION	: String = 'transition';
		
		public static const SP_INVERSE			: String = 'inverse';
		
		private var _model		: ObjectContainer3D;
		private var _objects	: Array;
		private var _fx			: FX;
		private var _cycleTimer	: Timer;
		private var _startParam	: Object;
		
		
		public function df_action(a_options: Object, a_model: ObjectContainer3D) {
			super(a_options);
			checkOption(FIELD_FPS, DEFAULT_FPS);
			checkOption(FIELD_CYCLE, DEFAULT_CYCLE);
			checkOption(FIELD_DURATION, DEFAULT_DURATION);
			checkOption(FIELD_TRANSITION, DEFAULT_TRANSITION);
			_objects 	= new Array();
			_model 		= a_model;
			_fx			= new FX(doFXupdate, a_options[FIELD_TRANSITION], options[FIELD_FPS], options[FIELD_DURATION]);

			initObjects(a_options.objects);
			if (options[FIELD_CYCLE]) startCycle();
		}
		
		public function get name(): String {
			return options.name;
		}
		
		override public function dispose():void {
			_fx.dispose();
			if (_cycleTimer) {
				_cycleTimer.stop();
				_cycleTimer.removeEventListener(TimerEvent.TIMER, doCycleTimer);
			}
			super.dispose();
		}
		
		protected function initObjects(a_objects: Object): void {
			_objects = new Array();
			if (a_objects) {
				if (a_objects is String) a_objects = (a_objects as String).split(',');
				(a_objects as Array).forEach(function (name: String, i: int, arr: Array): void {
					var child: ObjectContainer3D = df_awayUtils.objectByName(_model, name);
					if (child) _objects.push(child);
				})
			} else {
				for (var i:int=0; i<_model.numChildren; i++)
					_objects.push(_model.getChildAt(i));
			}
		}
		
		protected function get objects(): Array {
			return _objects;
		}
		
		public function get model(): ObjectContainer3D {
			return _model;
		}
		
		protected function get startParam(): Object {
			return _startParam;
		}
		
		public function start(a_startParam: Object=null): void {
/*			function doComplete(e: FXEvent): void {
				_fx.removeEventListener(FXEvent.COMPLETE, doComplete);
				_startParam = a_startParam;
				_fx.start();
			}*/
			if (!_fx.running) {
				_startParam = a_startParam;
				_fx.start();
			} //else _fx.addEventListener(FXEvent.COMPLETE, doComplete);
		}
		
		public function startCycle(): void {
			_cycleTimer = df_spaceObject.createTimer(1000 / options[FIELD_FPS], doCycleTimer);
			_cycleTimer.start();
		}
		
		protected function doCycleTimer(e: TimerEvent): void {
			update((_cycleTimer.currentCount * _cycleTimer.delay) / options[FIELD_DURATION]);
		}
		
		public function stop(): void {
			_fx.cancel();
		}
		
		private function doFXupdate(value: Number): void {
			var percent: Number = value
			if (startParam	== df_action.SP_INVERSE) percent = 1 - value;
			update(percent);
		}
		
		protected function update(percent: Number): void {
		}
	}
}